import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_clx_base/utils/toast_util.dart';

/// 再次点击关闭应用widget
class ExitWidget extends StatefulWidget {
  const ExitWidget({super.key, required this.child});

  final Widget child;

  @override
  State<ExitWidget> createState() => _ExitWidgetState();
}

class _ExitWidgetState extends State<ExitWidget> {
  DateTime? currentBackPressTime;

  @override
  Widget build(BuildContext context) {
    return PopScope(
      canPop: false,
      onPopInvoked: _onBackPressed, child: widget.child);
  }

  Future<bool> _onBackPressed(d) {
    DateTime now = DateTime.now();
    if (currentBackPressTime == null ||
        now.difference(currentBackPressTime!) > const Duration(seconds: 2)) {
      currentBackPressTime = now;
      ToastUtil.showToast("再次点击退出应用");
      return Future.value(false);
    }
    return exit(0);
  }
}
